from stark.service.v1 import StarkHandler, get_choice_text, get_date_format_text
from django.urls import re_path
from .base import PermissionHandler

class CheckPaymentRecordHandler(PermissionHandler,StarkHandler):
    order_list = ['-id', 'confirm_status']
    field_name_list = [StarkHandler.display_checkbox, 'customer', get_choice_text('费用类型', 'pay_type'),
                       get_date_format_text('申请日期', 'apply_date', format='%Y-%m-%d %X'), 'paid_fee', 'class_list',
                       'consultant',
                       get_choice_text('缴费状态', 'confirm_status')]

    def get_field_name_list(self,request,*args,**kwargs):
        value = []
        if self.field_name_list:
            value.extend(self.field_name_list)  # 去除编辑和删除操作
        return value

    def get_add_btn(self, request, *args, **kwargs):
        """去除添加按钮"""
        return None

    def get_urls(self):
        patterns = [
            re_path('list/', self.wrapper(self.changelist_view), name=self.get_list_url_name)
        ]
        patterns.extend(self.extra_urls())  # 新增的url 扩增
        return patterns

    """增加批量审核、批量驳回"""

    def multi_confirm(self, request, *args, **kwargs):
        pk_list = request.POST.getlist('pk')
        # 循环获取缴费记录表 、学生表、客户表，将每个表中的 状态信息修改
        for pk in pk_list:
            # 1. 修改缴费记录表中的缴费状态为已确认,保证原来的是申请中状态
            payment_obj = self.model_class.objects.filter(id=pk, confirm_status=1).first()
            if not payment_obj:
                continue
            payment_obj.confirm_status = 2
            payment_obj.save()
            # 2. 找到关联的客户表customer中的状态 改成已报名
            payment_obj.customer.status = 1
            payment_obj.customer.save()
            # 3. 关联的学生表中的学生状态由申请中改为在读
            payment_obj.customer.student.student_status = 2
            payment_obj.customer.student.save()

    multi_confirm.text = '批量审核'

    def multi_cancel(self, request, *args, **kwargs):
        # 状态改成已驳回
        pk_list = request.POST.getlist('pk')
        self.model_class.objects.filter(id__in=pk_list, confirm_status=1).update(confirm_status=3)

    multi_cancel.text = '批量驳回'

    action_list = [multi_confirm, multi_cancel]
